/
DefaultAnimationsEditorWindow.cs
86 lines (72 loc) · 2.8 KB
/
DefaultAnimationsEditorWindow.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System.Linq;
using UnityEditor;
using UnityEngine;
/// <summary>
/// Default animations editor window.
/// http://diegogiacomelli.com.br/coding-an-editorwindow-to-create-default-animations-from-sprites/
/// </summary>
public class DefaultAnimationsEditorWindow : EditorWindow
{
DefaultAnimationsSettings _settings;
SerializedObject _settingsSO;
SerializedProperty _animatorController;
SerializedProperty _animationsMapping;
SerializedProperty _animationsFolder;
SerializedProperty _spritesheets;
bool _showSettings;
bool _showOperations;
[MenuItem("Window/Default Animations")]
public static void ShowWindow()
{
GetWindow<DefaultAnimationsEditorWindow>("Default Animations");
}
private void OnEnable()
{
// Read the settings from the ScriptableObject and initialize the
// SeleriazedObject and SerializedProperty.
_settings = DefaultAnimationsSettings.Instance;
_settingsSO = new SerializedObject(_settings);
_animatorController = _settingsSO.FindProperty("AnimatorController");
_animationsMapping = _settingsSO.FindProperty("AnimationsMapping");
_animationsFolder = _settingsSO.FindProperty("AnimationsFolder");
_spritesheets = _settingsSO.FindProperty("Spritesheets");
}
private void OnGUI()
{
EditorGUI.BeginChangeCheck();
DrawSettingsSection();
DrawOperationsSection();
// If user made any change on editor window field, then
// apply the changes directly on scriptable object.
if (EditorGUI.EndChangeCheck())
_settingsSO.ApplyModifiedProperties();
}
private void DrawSettingsSection()
{
_showSettings = EditorGUILayout.BeginFoldoutHeaderGroup(_showSettings, "Settings");
if (_showSettings)
{
EditorGUILayout.PropertyField(_animatorController);
EditorGUILayout.PropertyField(_animationsMapping, true);
EditorGUILayout.PropertyField(_animationsFolder);
}
EditorGUILayout.EndFoldoutHeaderGroup();
}
private void DrawOperationsSection()
{
if (_animatorController.objectReferenceValue == null
|| _animationsMapping.arraySize == 0
|| string.IsNullOrEmpty(_animationsFolder.stringValue))
return;
_showOperations = EditorGUILayout.BeginFoldoutHeaderGroup(_showOperations, "Operations");
if (_showOperations)
{
EditorGUILayout.PropertyField(_spritesheets, true);
if (_settings.Spritesheets != null
&& _settings.Spritesheets.Length > 0
&& GUILayout.Button("Create animations"))
DefaultAnimationsUtility.Create(_settings.Spritesheets);
}
EditorGUILayout.EndFoldoutHeaderGroup();
}
}